package com.letu.database.mybatis.typehandler;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.letu.core.encrypt.RSAEncrypt;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.nio.charset.StandardCharsets;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * M9算法加密数据
 */
@Slf4j
@MappedJdbcTypes(JdbcType.VARCHAR)
public class M9EncryptHandler extends BaseTypeHandler<String> {
    /**
     * 线上运行后勿修改，会影响已加密数据解密
     */
    private static final byte[] KEYS = "efc987654321camp".getBytes(StandardCharsets.UTF_8);

    public static final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALX1mN9u2pZh6UzFmLwSN0QQAdybGeGRCHla/udRoHmctYjkn5zrS1IKMwRGIomjZDH4C4uRaQXSugP+s2MHTuzMgX/AOV39RZpfdPGNmxXnCHpXLueU/gI5nxyUEXNu7UzuveV616wlP0WrFQ6VDliq6/8F6KF5DJilnzhMuidTAgMBAAECgYBrolcGvD1DPbHSvwQFJEVa1pkzVPAvVSqNFr24ObnSlldwHcUo/qp0YiIhghdGEcfgIxk5Ow/D6Q4t9MlKZ/VP8HGf9jAEOtGNwis0ffy36Kex3pvKcPlllj+pOS+SqUo+iwJvaBPdPetLJC1nrAkufK7xyBN8229U0moVysV4wQJBAOCkJEAVKMU4ECzC0r9LGC9+p1jpD9tuf3xxHR3lHgkzcFghX/UHTVg3TYg8dkeIhB6ZraVbVMHjaE6RFJOWVqECQQDPXCmtj5KD20UPhoah4DGPGc+tH71l5QXk/1qXeDqv2E4fHsfRqszzGotwQYUSAzOvuhpWHda14OlGN38JwR1zAkA1V4SsGtolipx8pH56ohBKJt8CA8WaK3DmUc3CLYemU6qXosUwX92D1w34DbNgZRBKW9+Z5kgICkHYdibWrNpBAkAk9+VmUAZrA5iYX/94tKOhQ5TG5LW7iM04Ol5ISuMLnqCu46n0M2M+pCjf5o5226NHXQJ8/+zbmDF+lz5K2ucJAkBztOjeAXW3iEA7r7BZMhoQMhNN1YDIqJpNAoLU5teFnCzgQJZKNoPcK6oanV76TYWwl3fFvQPsVSH1LZzftEZ//";

    public static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC19ZjfbtqWYelMxZi8EjdEEAHcmxnhkQh5Wv7nUaB5nLWI5J+c60tSCjMERiKJo2Qx+AuLkWkF0roD/rNjB07szIF/wDld/UWaX3TxjZsV5wh6Vy7nlP4COZ8clBFzbu1M7r3letesJT9FqxUOlQ5Yquv/BeiheQyYpZ84TLonUwIDAQAB";


    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        if (StringUtils.isEmpty(parameter)) {
            ps.setString(i, null);
            return;
        }
//        try {
//            Long l = System.currentTimeMillis();
//            String encrypt = RSAEncrypt.encrypt(parameter, publicKey);
//            ps.setString(i, encrypt);
//            log.info("sm2111111 加密时间:{}", (System.currentTimeMillis() - l));
//        } catch (Exception e) {
//            e.printStackTrace();
//            ps.setString(i, null);
//        }
        ps.setString(i, TypeHandlerUtil.crypt(parameter));

    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {

        return TypeHandlerUtil.decrypt(rs.getString(columnName));
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return TypeHandlerUtil.decrypt(rs.getString(columnIndex));
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return TypeHandlerUtil.decrypt(cs.getString(columnIndex));
    }

}
